package cn.itcast.service.impl;

import cn.itcast.mapper.UserMapper;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.utils.MapperUtils;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService{
    private UserMapper userMapper =(UserMapper) MapperUtils.getMapper(UserMapper.class);
    @Override
    public List<User> findAll() {
        List<User> list = userMapper.findAll();
        MapperUtils.close();
        return list;
    }

    @Override
    public int insertAdd(User user) {
        //这里的user对象中的成员变量赋值不全，需要处理user对象中id
        String uid = UUID.randomUUID().toString().replaceAll("-", "");
        user.setId(uid);
        int count = userMapper.insertAdd(user);
        MapperUtils.close();
        return count;
    }

    @Override
    public void deleteById(String uid) {
        String[] uids = uid.split(",");
        if(uids != null && uids.length > 0) {
                userMapper.deleteById(uids);
        }
        MapperUtils.close();
    }

    @Override
    public User findById(String uid) {
       User u = userMapper.findById(uid);
        MapperUtils.close();
        return u;
    }

    @Override
    public void updateByUser(User user) {
        userMapper.updateById(user);
        MapperUtils.close();
    }


}
